package com.mocha.bss.domain.general.contoller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coffee.core.CoffeePage;
import com.coffee.core.CoffeeResponse;
import com.coffee.log.capture.OperationLog;
import com.coffee.log.dispose.OperationEntity;
import com.coffee.log.dispose.OperationRepository;
import com.coffee.mybatis.CoffeeDynamicTableNameContextHolder;
import com.coffee.mybatis.PageHandler;
import com.mocha.bss.domain.general.contoller.query.LogQuery;
import com.pinghua.mocha.basics.enumerate.MochaSocpEnum;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author songkui
 * @since 2025/5/14
 */
@Slf4j
@RestController
@RequestMapping("/log")
@AllArgsConstructor
public class LogController {

    private final OperationRepository operationRepository;

    // 应急  简单处理
    /** 监管APP-日志信息 */
//    @OperationLog(scope = "PC", name = "监管APP-日志信息")
    @PostMapping("/app-page")
    public CoffeeResponse<CoffeePage<OperationEntity>> pageByApp(@RequestBody LogQuery<OperationEntity> query){
        CoffeeDynamicTableNameContextHolder.setTableName(MochaSocpEnum.REG_APP.table());
        LambdaQueryWrapper<OperationEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotEmpty(query.getName()), OperationEntity::getName, query.getName());
        Page<OperationEntity> pageDate = operationRepository.page(query.buildPage(), queryWrapper);
        return CoffeeResponse.responseSuccess(PageHandler.buildCoffeePage(pageDate));
    }

    /** PC端-日志信息 */
//    @OperationLog(scope = "PC", name = "PC端-日志信息")
    @PostMapping("/pc-page")
    public CoffeeResponse<CoffeePage<OperationEntity>> pageByPC(@RequestBody LogQuery<OperationEntity> query){
        CoffeeDynamicTableNameContextHolder.setTableName(MochaSocpEnum.PC.table());
        LambdaQueryWrapper<OperationEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotEmpty(query.getName()), OperationEntity::getName, query.getName());
        Page<OperationEntity> pageDate = operationRepository.page(query.buildPage(), queryWrapper); //执行失败
        return CoffeeResponse.responseSuccess(PageHandler.buildCoffeePage(pageDate));
    }

}
